package com.csit.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.csit.bean.Pager;
import com.csit.bean.Pager.OrderType;
import com.csit.entity.DataDictionary;
import com.csit.entity.User;
import com.csit.util.StringUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @Description: Action基类
 * @Copyright: 福州骏华科技信息有限公司 (c)2012
 * @Created Date : 2012-3-29
 * @author longweier
 * @vesion 1.0
 */
@SuppressWarnings("unchecked")
public class BaseAction extends ActionSupport {

	private static final long serialVersionUID = 6718838822334455667L;
	//添加
	public static final String ADD = "add";
	//更新
	public static final String UPDATE = "update";
	//删除
	public static final String DELETE = "delete";
	//重定向返回列表
	public static final String VIEW = "view";
	//转发返回列表
	public static final String LIST = "list";
	//更新状态
	public static final String STATUS = "status";
	//前台提示图标
	public static final String ICON = "icon";	
	//警告
	public static final String WARN = "warning";
	//错误
	public static final String ERROR = "error";
	//信息
	public static final String MESSAGE = "message";
	//内容
	public static final String CONTENT = "content";
	//编辑
	public static final String EDIT = "edit";
	//版本好
	public static final String VERSION = "version";
	//当前台支持多选时接受参数
	protected String ids;
	//当前界面进入时Id
	protected String oldIds;
	//当前页
	protected Integer page;;
	//默认显示多少条
	protected Integer rows;
	//排序列 默认排序为创建时间
	protected String sort = "createDate";
	//排序方式 默认排序方式为降序
	protected String order = "desc";
	//分页的时候用,默认需要做一下转换,相当于是Adapter
	protected Pager pager = new Pager();
	//查询出来的一些结果期
	protected List list;
	//如果查询的时候只有一个条件的情况下用
	protected String property;
	// 日志记录信息
	protected String logInfo;
	// 操作提示后的跳转URL,为null则返回前一页
	protected String redirectionUrl;
	
	protected  String q;
	
	//返回input
	public String input() {
		return null;
	}

	// 获取Request里面的属性
	public Object getAttribute(String name) {
		return ServletActionContext.getRequest().getAttribute(name);
	}

	// 设置Request里面的属性
	public void setAttribute(String name, Object value) {
		ServletActionContext.getRequest().setAttribute(name, value);
	}

	// 获取Parameter
	public String getParameter(String name) {
		return getRequest().getParameter(name);
	}

	// 获取Parameter数组
	public String[] getParameterValues(String name) {
		return getRequest().getParameterValues(name);
	}

	// 通过name获取Session里面的值
	public Object getSession(String name) {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		return session.get(name);
	}

	// 获取Session
	public Map<String, Object> getSession() {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		return session;
	}

	// 给Session里面设置key-value
	public void setSession(String name, Object value) {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		session.put(name, value);
	}

	// 获取Request对象
	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	// 获取Response对象
	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	// 获取Application对象
	public ServletContext getApplication() {
		return ServletActionContext.getServletContext();
	}

	// AJAX输出，返回null
	public String ajax(String content, String type) {
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType(type + ";charset=UTF-8");
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			response.setCharacterEncoding("UTF-8");
			response.getWriter().write(content);
			response.getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	// AJAX输出文本，返回null
	public String ajaxText(String text) {
		return ajax(text, "text/plain");
	}

	// AJAX输出HTML，返回null
	public String ajaxHtml(String html) {
		return ajax(html, "text/html");
	}

	// AJAX输出XML，返回null
	public String ajaxXml(String xml) {
		return ajax(xml, "text/xml");
	}

	// 根据字符串输出JSON，返回null
	public String ajaxJson(String jsonString) {
		return ajax(jsonString, "text/plain");
	}
	
	// 根据Map输出JSON，返回null
	public String ajaxJson(Map<String, String> jsonMap) {
		JSONObject jsonObject = JSONObject.fromObject(jsonMap);
		return ajax(jsonObject.toString(), "text/html");
	}
	
	// 输出JSON警告消息，返回null
	public String ajaxJsonWarnMessage(String message) {
		Map<String, String> jsonMap = new HashMap<String, String>();
		jsonMap.put(STATUS, WARN);
		jsonMap.put(MESSAGE, message);
		JSONObject jsonObject = JSONObject.fromObject(jsonMap);
		return ajax(jsonObject.toString(), "text/html");
	}
	
	// 输出JSON成功消息，返回null
	public String ajaxJsonSuccessMessage(String message) {
		Map<String, String> jsonMap = new HashMap<String, String>();
		jsonMap.put(STATUS, SUCCESS);
		jsonMap.put(MESSAGE, message);
		JSONObject jsonObject = JSONObject.fromObject(jsonMap);
		return ajax(jsonObject.toString(), "text/html");
	}
	
	// 输出JSON错误消息，返回null
	public String ajaxJsonErrorMessage(String message) {
		Map<String, String> jsonMap = new HashMap<String, String>();
		jsonMap.put(STATUS, ERROR);
		jsonMap.put(MESSAGE, message);
		JSONObject jsonObject = JSONObject.fromObject(jsonMap);
		return ajax(jsonObject.toString(), "text/html");
	}
	
	// 设置页面不缓存
	public void setResponseNoCache() {
		getResponse().setHeader("progma", "no-cache");
		getResponse().setHeader("Cache-Control", "no-cache");
		getResponse().setHeader("Cache-Control", "no-store");
		getResponse().setDateHeader("Expires", 0);
	}
	
	//转码
	protected String decodeParameter(String parameter){
		if(StringUtil.isNull(parameter)){
			return null;
		}
		String result = null;
		try {
			result = URLDecoder.decode(parameter, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public String getProperty() {
		return property;
	}

	public void setProperty(String property) {
		if(StringUtil.isNotNull(property)){
			pager.setProperty(property);
		}
		this.property = property;
	}

	public String getLogInfo() {
		return logInfo;
	}

	public void setLogInfo(String logInfo) {
		this.logInfo = logInfo;
	}

	public String getRedirectionUrl() {
		return redirectionUrl;
	}

	public void setRedirectionUrl(String redirectionUrl) {
		this.redirectionUrl = redirectionUrl;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		if(page!=null&&page.intValue()>0){
			pager.setPageNumber(page);
		}
		this.page = page;
	}

	public Integer getRows() {
		return rows;
	}

	public void setRows(Integer rows) {
		if(rows!=null&&rows.intValue()>0){
			pager.setPageSize(rows);
		}
		this.rows = rows;
	}
	
	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		if(StringUtil.isNotNull(sort)){
			pager.setOrderBy(sort);
		}
		this.sort = sort;
	}

	public String getOrder() {
		return order;
	}
	//给pager也同时设置值
	public void setOrder(String order) {
		if(StringUtil.isNotNull(order)){
			pager.setOrderType(OrderType.valueOf(order));
		}
		this.order = order;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getIds() {
		return ids;
	}

	public String getOldIds() {
		return oldIds;
	}

	public void setOldIds(String oldIds) {
		this.oldIds = oldIds;
	}
	
	public String getQ() {
		return q;
	}

	public void setQ(String q) {
		this.q = q;
	}
	
	public DataDictionary getShop(){
		Integer shopID = (Integer) getSession(User.USERSHOP);
		DataDictionary shop = new DataDictionary();
		shop.setDataDictionaryID(shopID);
		return shop;
	}
}
